package agenceVoyageVirtuel.client.control;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;


public class ConnexionPanel extends Composite implements MouseOverHandler, MouseOutHandler, KeyPressHandler{

	private final EspacePersoPanier espacePersoPanier;
	private final DisclosurePanel panel;
	private final TextBox user;
	private final PasswordTextBox pass;
//	private boolean canBeClose;
//	private Animation timeOut;
//	private boolean launchedTimeOut = false;

	public ConnexionPanel(EspacePersoPanier espacePersoPanier) {
		
		this.espacePersoPanier = espacePersoPanier;
		
		FocusPanel focusPanel = new FocusPanel();
		initWidget(focusPanel);
		panel = new DisclosurePanel("Espace de Connexion");
		focusPanel.add(panel);
		panel.setSize("500", "300");
		
		Grid grid = new Grid(3, 2);
		panel.setContent(grid);
		grid.setSize("500","300");
		
		Label lblNewLabel = new Label("Utilisateur : ");
		grid.setWidget(0, 0, lblNewLabel);
		lblNewLabel.setHeight("100%");
		
		user = new TextBox();
		grid.setWidget(0, 1, user);
		user.setSize("100%", "100%");
		
		Label lblNewLabel_1 = new Label("Mot de Passe : ");
		grid.setWidget(1, 0, lblNewLabel_1);
		lblNewLabel_1.setHeight("100%");
		
		
		pass = new PasswordTextBox();
		grid.setWidget(1, 1, pass);
		pass.setSize("100%", "100%");
		
		grid.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		grid.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		
		Label message = new Label("");
		grid.setWidget(2, 0, message);
		
		Button btnConnexion = new Button("Connexion");
		btnConnexion.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				login();
			}
		});
		grid.setWidget(2, 1, btnConnexion);
		btnConnexion.setSize("100%", "30");
		
		focusPanel.addKeyPressHandler(this);
		user.addKeyPressHandler(this);
		pass.addKeyPressHandler(this);
		focusPanel.addMouseOverHandler(this);
		focusPanel.addMouseOutHandler(this);
	}

	@Override
	public void onMouseOver(MouseOverEvent event) {
		panel.setOpen(true);
//		canBeClose = true;
	}

	@Override
	public void onMouseOut(MouseOutEvent event) {
//		timeOut = new Animation() {
//			
//			@Override
//			protected void onUpdate(double progress) {
//				 if(progress==1){
//					 if(canBeClose){
//						 launchedTimeOut=false;
//						 panel.setOpen(false);
//					 }
//					 else {
//						 launchedTimeOut=true;
//						 panel.setOpen(true);
//						 canBeClose = true;
//						 run(5000);
//					 }
//				 }
//			}
//		};
//		if(!launchedTimeOut){
//			timeOut.run(5000);
//			launchedTimeOut = true;
//		}
	}

	@Override
	public void onKeyPress(KeyPressEvent event) {
//		timeOut.cancel();
//		canBeClose = false;
//		launchedTimeOut = false;
	}

	private void login() {
		espacePersoPanier.login(user.getText(),pass.getText());
	}

	public void clearPass() {
		pass.setText("");
	}

}
